package p2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-07
 * Time: 15:46
 */
public class d2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            Thread cur = Thread.currentThread();//获取到线程的引用,才能判断,currentThread是Thread类的静态方法,获取线程的引用(相当于t,但是,这里t还没创建好)
            while (!cur.isInterrupted()) {
                //Thread类里面有一个变量:isInterrupted,初始值是false,表示线程未被终止,如果其他线程调用interrupt,就会设为true
                System.out.println("Hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("执行到catch操作");
                    break;
                }
            }
        });
        t.start();
        Thread.sleep(1000);
        t.interrupt();//在主线程中控制t线程被终止,
    }
}
